import bosanova.*
class BootStrap {
    def sessionFactory
 
    def init = { servletContext ->
        configRoles()
        addAdministrator()
        uploadsalones(servletContext)
        uploadhorario(servletContext)
        uploadteachers(servletContext)
        uploadcursos(servletContext)
        uploadstudents(servletContext)  
        
    }
    
    def destroy = {
    }
    private void configRoles() {
        if(Rol.count() == 0){
            for(def i in Authority.values()){
                new Rol(authority: i.toString()).save()
            }
        }
    }

    
    private void uploadteachers (servletContext) {
            
        def session = sessionFactory.currentSession
        def entity
        def testDir = '/../Lists'
        
        def reader_teachers = new File(servletContext.getRealPath("${testDir}/Teacher.csv")).toCsvMapReader()
        reader_teachers.each {
            if(!Empleado.findByUsername(it.cedula)){  
                entity = new Empleado ()                           
                entity.identificacion = it.cedula
                entity.username = it.cedula
                entity.password = it.cedula
                entity.nombres = it.nombres
                entity.apellidos = it.nombres
                entity.celular = '3124567890'
                entity.telefono = '2345678'
                entity.gruposanguineo = 'O+'
                entity.nacionalidad ='COLOMBIA'
                entity.nacimiento = new Date()
                entity.genero = 'M'
                entity.direccion = it.direccion
                entity.email = it.correo
                entity.enabled = true         
                entity.save(validate: false)
                def rol = Rol.findByAuthority(Authority.ROLE_PROFESOR.toString())
                UsuarioRol.create(entity, rol)
            }
        }
    }
    private void uploadstudents (servletContext) {
            
        def session = sessionFactory.currentSession
        def entity
        def testDir = '/../Lists'
        def parent          
        def reader_students = new File(servletContext.getRealPath("${testDir}/Students.csv")).toCsvMapReader()
        reader_students.each {
            if(!Estudiante.findByUsername(it.ID)){
                parent = Curso.findWhere(cursoId: it['Curso'],jornada:it['Jornada'])
                entity = new Estudiante ()           
                entity.identificacion = it.ID
                entity.username = it.ID
                entity.password = it.ID
                entity.nombres = it.Nombre
                entity.apellidos = it.Nombre
                entity.celular = '3124567890'
                entity.telefono = '2345678'
                entity.gruposanguineo = 'O+'
                entity.nacionalidad ='COLOMBIA'
                entity.nacimiento = new Date()
                entity.genero = 'M'
                entity.direccion = "calle"+it.ID
                entity.email = 'user'+it.ID+'@mail.com'
                entity.enabled = true
                entity.curso= parent
                entity.save(validate: false)
                def rol = Rol.findByAuthority(Authority.ROLE_ESTUDIANTE.toString())
                UsuarioRol.create entity, rol , true  
                //UsuarioRol.create(entity, rol)
            }
        }
            
    }
    private void addAdministrator () {
        if(!Usuario.findByUsername('admin')){
            def entity = new Usuario()
            entity.identificacion = 'ADMIN'
            entity.username = 'admin'
            entity.password = 'admin123'
            entity.nombres = 'Jorge'
            entity.apellidos = 'Ovalle'
            entity.celular = '3124567890'
            entity.telefono = '2345678'
            entity.gruposanguineo = 'O+'
            entity.nacionalidad ='COLOMBIA'
            entity.nacimiento = new Date()
            entity.genero = 'M'
            entity.direccion = 'calle falsa 123'
            entity.email = 'admin@mail.com'
            entity.enabled = true
            entity.save(validate: false)
            def rol = Rol.findByAuthority(Authority.ROLE_ADMIN.toString()) 
            //UsuarioRol.create(entity, rol)
            UsuarioRol.create entity, rol , true 
        }
        
    }
    private void uploadcursos (servletContext) {
            
        def session = sessionFactory.currentSession
        def entity
        def testDir = '/../Lists'
                    
        def reader_students = new File(servletContext.getRealPath("${testDir}/cursos.csv")).toCsvMapReader()
        reader_students.each {
                
            entity = new Curso ()           
            entity.cursoId=it.curso_id
            entity.jornada=it.jornada
            entity.directorGrupo= Empleado.findByUsername(it.director_grupo_id)
            entity.fechaCurso= new Date()
            entity.save(validate: false)
                    
        }
            
    }
    private void uploadsalones(servletContext) {
            
        def session = sessionFactory.currentSession
        def entity
        def testDir = '/../Lists'
                    
        def reader = new File(servletContext.getRealPath("${testDir}/salones.csv")).toCsvMapReader()
        reader.each {                
            entity = new EspacioFisico()           
            entity.IdEspacio=it.id_espacio
            entity.descripcion=it.descripcion
            entity.save(validate: false)                 
        }            
    }
    private void uploadhorario(servletContext) {
            
        def session = sessionFactory.currentSession
        def entity
        def testDir = '/../Lists'
                    
        def reader = new File(servletContext.getRealPath("${testDir}/horario.csv")).toCsvMapReader()
        reader.each {                
            entity = new Horario()           
            entity.dia=it.dia
            entity.horaInicio=it.horainicio
            entity.horaFin=it.horafin
            entity.save(validate: false)                 
        }            
    }
}      